﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair
{
    public partial class Frmjcxf : Form
    {
        public Frmjcxf()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            chazhao();
            
        }
        public void chazhao() 
        {
            if (this.txtcardid.Text.Equals(string.Empty))
            {
                MessageBox.Show("请输入卡号");
            }
            else 
            {
                string sqlcx = string.Format("SELECT  items.iname,[jcnum] FROM [BHairDB].[dbo].[csmx] join items on csmx.jcspid=items.iid where cardID='{0}'", this.txtcardid.Text.ToString());
                string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(constr);
                try
                {
                    conn.Open();
                    SqlDataAdapter sda = new SqlDataAdapter(sqlcx, conn);
                    DataSet ds = new DataSet();
                    sda.Fill(ds, "csmx");
                    this.cmbxm.DataSource = ds.Tables["csmx"];
                    this.cmbxm.DisplayMember = "items.iname";
                    conn.Close();
                    select1();

                }
                catch (Exception ex)
                {
                    conn.Close();
                    MessageBox.Show(ex.Message);
                }
            }
        }
        public void select1()
        {
            string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
            SqlConnection conn1 = new SqlConnection(constr);
            string sqlselect = string.Format("select spname as 项目名称,jcnum as 剩余次数 from csmx inner join jc on csmx.jcspid=jc.id where cardID='{0}'", this.txtcardid.Text);
            try
            {
                conn1.Open();
                SqlDataAdapter sda1 = new SqlDataAdapter(sqlselect,conn1);
                DataTable dt1 = new DataTable();
                sda1.Fill(dt1);
                this.skinDataGridView2.DataSource = dt1;
                conn1.Close();
            }
            catch (Exception e1)
            {
                conn1.Close();
                MessageBox.Show(e1.Message);
            }
        }

        private void Frmjcxf_Load(object sender, EventArgs e)
        {
            string bs = "jc-";
            string time = DateTime.Now.ToLongDateString();
            Random ran = new Random();
            int RandKey = ran.Next(10, 9999);
            string bh = bs +time+"-"+RandKey.ToString();
            this.txtbh.Text = bh;
        }

        private void button2_Click(object sender, EventArgs e)
        {
           /* string xm = this.cmbxm.Text;
            int cs;
            MessageBox.Show(xm);
            if (this.txtcs.Text.Equals(string.Empty))
            {
                MessageBox.Show("请填写次数");
            }
            else 
            {
                 cs = Int32.Parse(this.txtcs.Text.Trim());
            
            string bh = this.txtbh.Text;
            string time1 = DateTime.Now.ToLongDateString();
            string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
            SqlConnection conn2 = new SqlConnection(constr);
            string sqlmx =string.Format( "INSERT INTO [BHairDB].[dbo].[jcxf]"
                + "           ([djbh]"
                + "           ,[xmmc]"
                + "           ,[xfcs]"
                + "           ,[riqi])"
                + "     VALUES"
                + "           ('{0}'"
                + "           ,'{1}'"
                + "           ,{2}"
                + "           ,'{3}')",bh,xm,cs,time1);
            try 
            {
                conn2.Open();
                SqlCommand comm1 = new SqlCommand(sqlmx,conn2);
                comm1.ExecuteNonQuery();
                conn2.Close();
            }catch(Exception e2)
            {
                conn2.Close();
                MessageBox.Show(e2.Message);
            }
            }
            * */
            add();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void add() 
        {
            int index = this.skinDataGridView1.Rows.Add();
            this.skinDataGridView1.Rows[index].Cells[0].Value = this.cmbxm.Text;
            this.skinDataGridView1.Rows[index].Cells[1].Value = this.txtcs.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string djh = this.txtbh.Text;//单据号
            string time1 = DateTime.Now.ToLongDateString();
            int rowss = (this.skinDataGridView1.Rows.Count) - 1;
            int spid;
            string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
            SqlConnection conn5 = new SqlConnection(constr);
            SqlConnection conn10 = new SqlConnection(constr);
            
            for (int i = 0; i < rowss;i++ )
            {
                conn10.Open();
                string spname = this.skinDataGridView1.Rows[i].Cells[0].Value.ToString();
                string sql1 =string.Format("SELECT [id],[iid],[iname]FROM [BHairDB].[dbo].[items] where iname='{0}'",spname);
               // conn10.Open();
                SqlCommand comm10 = new SqlCommand(sql1,conn10);
                SqlDataReader sdr1 = comm10.ExecuteReader();
                int syc;   //剩余次数
                int csnum;  //消费次数
                
                while(sdr1.Read())
                {
                    spid =Int32.Parse( sdr1["id"].ToString().Trim());
                    int xfcs = Int32.Parse(this.skinDataGridView1.Rows[i].Cells[1].Value.ToString());
                    try
                    {
                        conn5.Open();
                        string sqlmx = string.Format("INSERT INTO [BHairDB].[dbo].[jcxf]"
                    + "           ([djbh]"
                    + "           ,[xmid]"
                    + "         ,[xmname]"
                    + "           ,[xfcs]"
                    + "           ,[riqi],[cardid],[bz])"
                    + "     VALUES"
                    + "           ('{0}'"
                    + "           ,{1}"
                    + "           ,'{2}'"
                    + "           ,{3},'{4}','{5}','{6}')", djh, spid,spname, xfcs, time1,this.txtcardid.Text,txtbz.Text);
                        SqlCommand comm5 = new SqlCommand(sqlmx, conn5);
                        comm5.ExecuteNonQuery();
                        conn5.Close();

                        string sqlselect =string.Format( "SELECT [jcspid],[jcnum] FROM [BHairDB].[dbo].[csmx] where cardID={0} and jcspid={1}",this.txtcardid.Text,spid);
                        //根据卡号和 商品ID查询项目的剩余次数
                        SqlConnection conn3=new SqlConnection(constr);
                        conn3.Open();
                        SqlCommand comm3=new SqlCommand(sqlselect,conn3);
                        SqlDataReader sdr3 = comm3.ExecuteReader();
                        while (sdr3.Read())
                        {
                            syc =Int32.Parse( sdr3["jcnum"].ToString().Trim());
                            csnum = syc - xfcs;
                            string sqlup = string.Format("UPDATE [BHairDB].[dbo].[csmx]"
                            + "   SET [jcnum] = {0}"
                            + " WHERE cardID={1} and jcspid={2}",csnum,this.txtcardid.Text, spid);
                            try
                            {
                                SqlConnection conn4 = new SqlConnection(constr);
                                conn4.Open();
                                SqlCommand comm4 = new SqlCommand(sqlup, conn4);
                                comm4.ExecuteNonQuery();
                                conn4.Close();

                            }
                            catch (Exception e4) 
                            {
                                MessageBox.Show(e4.Message);
                            }

                        }
                        

                    }
                    catch (Exception e3)
                    {
                        conn5.Close();
                        conn10.Close();
                        MessageBox.Show(e3.Message);
                    }
                    //conn10.Close();

                }

                conn10.Close();
            }
            MessageBox.Show("结算成功");
            shuaxin();
            


        }
        public void shuaxin() 
        {
            string bs = "jc-";
            string time = DateTime.Now.ToLongDateString();
            Random ran = new Random();
            int RandKey = ran.Next(10, 9999);
            string bh = bs +time+"-"+RandKey.ToString();
            this.txtbh.Text = bh;
            this.txtcardid.Text = null;
            this.txtcs.Text = null;
            this.cmbxm.DataSource = null;
            this.skinDataGridView1.Rows.Clear();
            this.skinDataGridView2.DataSource = null;

        }

        private void txtcardid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                chazhao();
            }
        }

        private void Frmjcxf_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.F1)
            {
                chazhao();
            }
        }

        

        

       

        

       
    }
}
